<script setup lang="ts">
import { ref, reactive, watch } from "vue"
import { CirclePlus, Refresh, RefreshRight, Search } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { dictDataDeleteApi, dictDataListApi } from "@/api/conventional"
import dataDialog from "./dataDialog.vue"
import { ElMessage, ElMessageBox, type FormInstance } from "element-plus"

const dialogVisible = ref(false)
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const dataDialogRef = ref<any>(null)

//#region 增
const handleAdd = () => {
  console.log("add")
  if (dataDialogRef.value) {
    const data = {
      type_code: searchData.type_code
    }
    dataDialogRef.value.showClick(data)
  }
}
//#endregion

//#region 增
const handleUpdate = (el: any) => {
  if (dataDialogRef.value) {
    dataDialogRef.value.showClick(el)
  }
  // dialogVisible.value = true
}
//#endregion

//#region 删
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`正在删除用户：${row.data_name}，确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    dictDataDeleteApi({ id: row.id }).then(() => {
      ElMessage.success("删除成功")
      getTableData()
    })
  })
  // dialogVisible.value = true
}
//#endregion

//#region 查
const tableData = ref<any[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  type_code: ""
})
const getTableData = () => {
  loading.value = true
  dictDataListApi({
    pageNum: paginationData.currentPage,
    pageSize: paginationData.pageSize,
    type_code: searchData.type_code
  })
    .then((res) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion
const handleClose = (done: () => void) => {
  done()
  closes()
}
const closes = () => {
  tableData.value = []
  searchData.type_code = ""
}
const showClick = (val: any) => {
  tableData.value = []
  searchData.type_code = val.type_code
  getTableData()
  dialogVisible.value = true
}
defineExpose({ showClick }) //暴露子组件的方法或者数据
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <el-dialog v-model="dialogVisible" title="数据管理" draggable destroy-on-close fullscreen :before-close="handleClose">
    <el-card class="box-card" v-loading="loading" shadow="never">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="username" label="用户名">
          <!-- <el-input v-model="searchData.username" placeholder="请输入" /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增</el-button>
          <!--                  <el-button type="danger" :icon="Delete">批量删除</el-button>-->
        </div>
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column property="type_code" label="类型代码" width="150" align="center" />
          <el-table-column property="data_code" label="数据类型" width="150" align="center" />
          <el-table-column property="data_name" label="数据名称" width="150" align="center" />
          <el-table-column property="data_value" label="数据值" width="150" align="center" />
          <el-table-column prop="status" label="是否启用" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.status == 1" type="success">启用</el-tag>
              <el-tag v-else type="danger">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <dataDialog ref="dataDialogRef" @do-sth="handleSearch" />
  </el-dialog>
</template>

<style scoped lang="scss">
.box-card {
  margin-bottom: 10px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
</style>
